package it.tava.andbudget.data;

import it.tava.andbudget.data.resource.Charge;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbManager implements DataManager {

	private static final String DATABASE_NAME = "andBudget.db";
	private static final String DATABASE_TABLE = "charges";
	private static final int DATABASE_VERSION = 1;

	private SQLiteDatabase db;
	private DbOpenHelper dbHelper;

	public DbManager(Context context) {
		dbHelper = new DbOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void open() {
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = dbHelper.getReadableDatabase();
		}
	}

	@Override
	public void close() {
		db.close();
	}
	
	@Override
	public long addCharge(Charge charge) {
		ContentValues newTaskValues = new ContentValues();
		
		newTaskValues.put("year", charge.getCategory());
		newTaskValues.put("month", charge.getCategory());
		newTaskValues.put("day", charge.getCategory());
		newTaskValues.put("category", charge.getCategory());
		newTaskValues.put("description", charge.getDescription());
		newTaskValues.put("cost", charge.getCost());
		
		return db.insert(DATABASE_TABLE, null, newTaskValues);
	}

	@Override
	public Charge getCharge(long id) {
		Cursor cursor = db.query(true, DATABASE_TABLE,
				new String[] { "id", "" }, "id" + "=" + id, null, null, null, null, null);
		
		if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No to do item found for row: " + id);
		}
		
		//TODO
		int _id;
		int year;
		short month;
		short day;
		String category;
		String description;
		float cost;
		
		Charge result = new Charge();
		
		return result;
	}

	@Override
	public boolean updateCharge(long id, Charge charge) {
		ContentValues newValue = new ContentValues();
		newValue.put("task", charge.getCategory());
		return db.update(DATABASE_TABLE, newValue, "id" + "=" + id, null) > 0;
	}
	
	@Override
	public boolean removeCharge(long id) {
		return db.delete(DATABASE_TABLE, "id=" + id, null) > 0;		
	}
	
	
	public Cursor getAllToDoItemsCursor() {
		return db.query(DATABASE_TABLE, new String[] { "id", "task", "creation" }, null, null, null, null, null);
	}

	public Cursor setCursorToToDoItem(long _rowIndex) throws SQLException {
		Cursor result = db.query(true, DATABASE_TABLE, new String[] { "id", "task" }, "id" + "=" + _rowIndex, null, null, null, null, null);
		
		if ((result.getCount() == 0) || !result.moveToFirst()) {
			throw new SQLException("No to do items found for row: " + _rowIndex);
		}
		
		return result;
	}

	private static class DbOpenHelper extends SQLiteOpenHelper {

		public DbOpenHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sqlCreate = "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE
					+ " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
					+ "YEAR INTEGER NOT NULL," + "MONTH INTEGER NOT NULL,"
					+ "DAY INTEGER NOT NULL," + "CATEGORY TEXT NOT NULL,"
					+ "DESCRIPTION TEXT NOT NULL," + "CHARGE ??? NOT NULL)";

			db.execSQL(sqlCreate);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("", "Upgrading from version " + oldVersion
					+ " to new version " + newVersion
					+ " will destroy all old data!");

			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

	}
}